package com.example.chasshelper.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.chasshelper.R;
import com.example.chasshelper.dao.ClassroomDao;
import com.example.chasshelper.dao.CourseDao;
import com.example.chasshelper.dao.MessageDao;
import com.example.chasshelper.dao.RegistrationDao;
import com.example.chasshelper.dao.SolutionDao;
import com.example.chasshelper.dao.StudentDao;
import com.example.chasshelper.dao.TeacherDao;
import com.example.chasshelper.dao.TestDao;
import com.example.chasshelper.model.Classroom;
import com.example.chasshelper.model.Message;
import com.example.chasshelper.model.Student;
import com.example.chasshelper.model.Teacher;
import com.example.chasshelper.utils.DaoUtils;

import java.util.ArrayList;
import java.util.List;

/*
* 从ClassroomTeaActivity或ClassroomStuActivity跳转过来
* */
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder> {
    private Context mContext;
    //数据
    private List<Message> list;
    private Classroom classroom;
    private Teacher teacher;
    private Student student;
    //dao
    private TeacherDao teacherDao= DaoUtils.getTeacherDao();
    private StudentDao studentDao=DaoUtils.getStudentDao();
    private CourseDao courseDao=DaoUtils.getCourseDao();
    private ClassroomDao classroomDao=DaoUtils.getClassroomDao();
    private RegistrationDao registrationDao=DaoUtils.getRegistrationDao();
    private SolutionDao solutionDao = DaoUtils.getSolutionDao();
    private MessageDao messageDao = DaoUtils.getMessageDao();
    private TestDao testDao = DaoUtils.getTestDao();

    private Teacher messageTeacher;
    private Student messageStudent;
    //接收数据
    public MessageAdapter(Context mContext, List<Message> list, Classroom classroom, Teacher teacher, Student student) {
        this.mContext = mContext;
        this.list = list;
        this.classroom = classroom;
        this.teacher = teacher;
        this.student = student;
    }

    public void setList(List<Message> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public MessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        MessageViewHolder messageViewHolder = new MessageViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_message_rv, parent, false));
        return messageViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MessageViewHolder holder, int position) {
        //回显
        Message message = list.get(position);
        messageTeacher = messageDao.findTeacher(message);
        messageStudent = messageDao.findStudent(message);
        System.out.println("教师：" + messageTeacher);
        System.out.println("学生：" + messageStudent);
        if (messageDao.findTeacher(message) == null) {//学生
            holder.tv_source.setText(messageStudent.getStudentName());
            if (student != null && student.equals(messageStudent)) {
                holder.tv_source.setTextColor(Color.BLACK);
            } else {
                holder.tv_source.setTextColor(Color.parseColor("#592E2D3C"));
            }
        } else {//教师
            holder.tv_source.setText("教师：" + messageTeacher.getTeacherName());
            if (teacher != null && teacher.equals(messageTeacher)) {
                holder.tv_source.setTextColor(Color.BLACK);
            } else {
                holder.tv_source.setTextColor(Color.parseColor("#592E2D3C"));
            }
        }
        holder.tv_content.setText("    "+message.getMessageContent());
        holder.tv_time.setText(message.getMessageTime());
        //点击事件
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MessageViewHolder extends RecyclerView.ViewHolder{
        //控件
        TextView tv_content;
        TextView tv_source;
        TextView tv_time;
        public MessageViewHolder(@NonNull View itemView) {
            super(itemView);
            //绑定控件
            tv_content = itemView.findViewById(R.id.tv_content);
            tv_source = itemView.findViewById(R.id.tv_source);
            tv_time = itemView.findViewById(R.id.tv_time);
        }
    }
}
